package com.csjbot.weedle_auto.view

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView

import com.csjbot.weedle_auto.R


/**
 * Created by chenqi on 8/11/17.
 */

class BaseDialog : Dialog {
    constructor(context: Context) : super(context)

    constructor(context: Context, theme: Int) : super(context, theme)


    //    public abstract int getResurseID();

    class Builder(private val context: Context) {
        private var title: String? = null
        private var message: String? = null
        private var positiveButtonText: String? = null
        private var negativeButtonText: String? = null
        private var contentView: View? = null
        private var positiveButtonClickListener: DialogInterface.OnClickListener? = null
        private var negativeButtonClickListener: DialogInterface.OnClickListener? = null

        fun setMessage(message: String): Builder {
            this.message = message
            return this
        }


        /**
         * Set the Dialog message from resource
         *
         * @param message
         * @return
         */
        fun setMessage(message: Int): Builder {
            this.message = context.getText(message) as String
            return this
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        fun setTitle(title: Int): Builder {
            this.title = context.getText(title) as String
            return this
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        fun setTitle(title: String): Builder {
            this.title = title
            return this
        }

        fun setCancleEnable(e: Boolean): Builder {
            mCancelable = e
            return this
        }

        fun setContentView(v: View): Builder {
            this.contentView = v
            return this
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        fun setPositiveButton(positiveButtonText: Int,
                              listener: DialogInterface.OnClickListener): Builder {
            this.positiveButtonText = context
                    .getText(positiveButtonText) as String
            this.positiveButtonClickListener = listener
            return this
        }

        fun setPositiveButton(positiveButtonText: String,
                              listener: DialogInterface.OnClickListener): Builder {
            this.positiveButtonText = positiveButtonText
            this.positiveButtonClickListener = listener
            return this
        }

        fun setNegativeButton(negativeButtonText: Int,
                              listener: DialogInterface.OnClickListener): Builder {
            this.negativeButtonText = context
                    .getText(negativeButtonText) as String
            this.negativeButtonClickListener = listener
            return this
        }

        fun setNegativeButton(negativeButtonText: String,
                              listener: DialogInterface.OnClickListener): Builder {
            this.negativeButtonText = negativeButtonText
            this.negativeButtonClickListener = listener
            return this
        }

        fun create(): BaseDialog {
            val inflater = context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            // instantiate the dialog with the custom Theme
            val dialog = BaseDialog(context, R.style.CusInputAlertDialog)
            val layout = inflater.inflate(R.layout.main_dialog, null)
            dialog.addContentView(layout, ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
            // set the dialog title
            (layout.findViewById(R.id.title) as TextView).text = title
            // set the confirm button
            if (positiveButtonText != null) {
                (layout.findViewById(R.id.positiveButton) as Button).text = positiveButtonText
                if (positiveButtonClickListener != null) {
                    (layout.findViewById(R.id.positiveButton) as Button)
                            .setOnClickListener {
                                positiveButtonClickListener!!.onClick(dialog,
                                        DialogInterface.BUTTON_POSITIVE)
                            }
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.negativeButton).visibility = View.GONE
            }
            // set the cancel button
            if (negativeButtonText != null) {
                (layout.findViewById(R.id.negativeButton) as Button).text = negativeButtonText
                if (negativeButtonClickListener != null) {
                    (layout.findViewById(R.id.negativeButton) as Button)
                            .setOnClickListener {
                                negativeButtonClickListener!!.onClick(dialog,
                                        DialogInterface.BUTTON_NEGATIVE)
                            }
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.negativeButton).visibility = View.GONE
            }
            // set the content message
            if (message != null) {
                (layout.findViewById(R.id.negativeButton) as TextView).text = message
            } else if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                (layout.findViewById(R.id.content) as LinearLayout)
                        .removeAllViews()
                (layout.findViewById(R.id.content) as LinearLayout).addView(
                        contentView, ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT))
            }
            //            dialog.setContentView(layout);
            //            DisplayMetrics dm = new DisplayMetrics();
            //            //获取屏幕信息
            //            ((FragmentActivity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
            //            int screenWidth = dm.widthPixels;
            //            int screenHeigh = dm.heightPixels;
            //            WindowManager.LayoutParams params = dialog.getWindow().getAttributes();//获取dialog信息
            //            params.width = screenWidth;
            //            params.height = screenHeigh / 4;
            //            dialog.getWindow().setAttributes(params);//设置大小
            return dialog
        }

    }

    companion object {


        protected var mCancelable = true
    }
}
